<?php

/**
 * Poliza form base class.
 *
 * @method Poliza getObject() Returns the current form's model object
 *
 * @package    Sanitas
 * @subpackage form
 * @author     Jose Antonio Sánchez Bedoya
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BasePolizaForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(),
      'poliza'           => new sfWidgetFormInputText(),
      'colectivo'        => new sfWidgetFormInputText(),
      'fecha_renovacion' => new sfWidgetFormDate(),
      'producto'         => new sfWidgetFormInputText(),
      'fecha_alta'       => new sfWidgetFormDate(),
      'estado'           => new sfWidgetFormInputText(),
      'fecha_estado'     => new sfWidgetFormDate(),
      'total_prima'      => new sfWidgetFormInputText(),
      'frecuencia_pago'  => new sfWidgetFormInputText(),
      'banco'            => new sfWidgetFormInputText(),
      'sucursal'         => new sfWidgetFormInputText(),
      'dc'               => new sfWidgetFormInputText(),
      'cuenta'           => new sfWidgetFormInputText(),
      'observaciones'    => new sfWidgetFormInputText(),
      'created_at'       => new sfWidgetFormDateTime(),
      'updated_at'       => new sfWidgetFormDateTime(),
      'validated_at'     => new sfWidgetFormDateTime(),
      'is_validated'     => new sfWidgetFormInputCheckbox(),
      'removed_at'       => new sfWidgetFormDateTime(),
      'remove_it'        => new sfWidgetFormInputCheckbox(),
      'contactos_list'   => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Contacto')),
    ));

    $this->setValidators(array(
      'id'               => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'poliza'           => new sfValidatorInteger(),
      'colectivo'        => new sfValidatorInteger(),
      'fecha_renovacion' => new sfValidatorDate(),
      'producto'         => new sfValidatorString(array('max_length' => 150)),
      'fecha_alta'       => new sfValidatorDate(),
      'estado'           => new sfValidatorString(array('max_length' => 150)),
      'fecha_estado'     => new sfValidatorDate(),
      'total_prima'      => new sfValidatorNumber(),
      'frecuencia_pago'  => new sfValidatorString(array('max_length' => 150)),
      'banco'            => new sfValidatorInteger(),
      'sucursal'         => new sfValidatorInteger(),
      'dc'               => new sfValidatorInteger(),
      'cuenta'           => new sfValidatorInteger(),
      'observaciones'    => new sfValidatorString(array('max_length' => 250)),
      'created_at'       => new sfValidatorDateTime(),
      'updated_at'       => new sfValidatorDateTime(),
      'validated_at'     => new sfValidatorDateTime(array('required' => false)),
      'is_validated'     => new sfValidatorBoolean(array('required' => false)),
      'removed_at'       => new sfValidatorDateTime(array('required' => false)),
      'remove_it'        => new sfValidatorBoolean(array('required' => false)),
      'contactos_list'   => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Contacto', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('poliza[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Poliza';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['contactos_list']))
    {
      $this->setDefault('contactos_list', $this->object->Contactos->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveContactosList($con);

    parent::doSave($con);
  }

  public function saveContactosList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['contactos_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Contactos->getPrimaryKeys();
    $values = $this->getValue('contactos_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Contactos', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Contactos', array_values($link));
    }
  }

}
